<?php
namespace App\Jobs;

use App\Jobs\Middleware\EasyJob;
use App\Library\Service\DB\TransactionMonitorService;
use App\Library\ThirdParty\DingTalk\DingTalkSendMsg;
use Illuminate\Bus\Queueable;

abstract class Job
{
    /*
    |--------------------------------------------------------------------------
    | Queueable Jobs
    |--------------------------------------------------------------------------
    |
    | This job base class provides a central location to place any logic that
    | is shared across all of your jobs. The trait included with the class
    | provides access to the "onQueue" and "delay" queue helper methods.
    |
    */

    use Queueable;

    protected $params;

    public function __construct(...$params)
    {
        $this->params = $params;
    }

    public function middleware()
    {
        return [new EasyJob];
    }

    /**
     * 任务失败的处理过程
     * @param \Exception $e
     * @return void
     */
    public function failed(\Exception $e)
    {
        easy_log('job_fail', ['job' => get_class($this), '入参' => $this->params, $e->getMessage(), $e->getFile(), $e->getLine()]);
        DingTalkSendMsg::sendErrorMsg(['job' => static::class, 'tip' => '队列执行异常'], $e);
    }

    /**
     * 记录日志
     * @param mixed $msg
     */
    public function log($msg)
    {
        $class_name = class_basename(static::class);
        $merchant_id = 0;
        if (isset($this->merchant_id)) {
            $merchant_id = (int)$this->merchant_id;
        }
        easy_log('job' . DIRECTORY_SEPARATOR . $merchant_id, $msg, $class_name);
    }

    public function getParams()
    {
        return $this->params;
    }
}